package com.wwcom.darteagle.data;

import java.util.List;

import com.wwcom.darteagle.model.DartStats;
import com.wwcom.darteagle.model.Match;
import com.wwcom.darteagle.model.MatchInfo;
import com.wwcom.darteagle.model.MatchLegs;
import com.wwcom.darteagle.model.MatchPlayer;
import com.wwcom.darteagle.model.MatchSets;
import com.wwcom.darteagle.model.VisitScore;

import android.database.sqlite.SQLiteDatabase;

public class MatchObject extends DataObject {    
    private VisitScoreObject visitScoreObj;
    private MatchInfoObject matchInfoObj;
    private DartStatsObject dartStatsObj;
    
    public MatchObject(SQLiteDatabase database) {
    	super(database);
    	visitScoreObj= new VisitScoreObject(database);
    	matchInfoObj= new MatchInfoObject(database);
    	dartStatsObj= new DartStatsObject(database);
    }
    
	static public String createSQL() {
		return null;
	}

	static public String deleteSQL() {
		return null;
	}
    
    static public String upgradeSQL(int oldVersion, int newVersion) {
		return null;	
	}
    
        
    public Match get(long matchId) {
    	MatchInfo mi= matchInfoObj.get(matchId);
    	//reset match player scores because we're going to load all the info
    	for (MatchPlayer mp : mi.getMatchPlayers()) {
    		mp.setNumLegs(0);
    		mp.setNumSets(0);
    	}
    	
    	Match m;
    	if (mi.getNumSets() > 0)
    		m= new MatchSets(mi);
    	else
    		m= new MatchLegs(mi);
    	m.startPlaying();    	
    	
    	//load match history
    	visitScoreObj.getAll(m,matchId);
    	    	
    	return m;
    }
    
    
    public void insert(Match m) {
    	    	
    	MatchInfo mi= m.getMatchInfo();
    	matchInfoObj.insert(m);

        int visitScoreOrder= 0;
        for (VisitScore vs : m.getAllVisitScoresInVisitOrder()) {
        	if (vs != null && vs.isFinalised()) {
        		visitScoreObj.insert(vs,mi.getId(),visitScoreOrder);
        		visitScoreOrder++;
        	}        	
        }
        
        //Save all match stats into database
        List<DartStats> dsList= m.getPlayerStats();
        for (DartStats ds : dsList) {
        	dartStatsObj.insert(ds);
        }
        /* No longer saving leg and set stats directly - just match stats
        if (m.getNumSets() > 0) {
        	for (Set s : m.getSets()) {
                List<DartStats> dsList2= s.getPlayerStats();
                for (DartStats ds : dsList2) {
                	dartStatsObj.insert(ds);
                }
                for (Leg l : s.getLegs()) {
	                List<DartStats> dsList3= l.getPlayerStats();
	                for (DartStats ds : dsList3) {
	                	dartStatsObj.insert(ds);
	                }
                }
        	}        	
        } else {
            for (Leg l : m.getLegs()) {
                List<DartStats> dsList4= l.getPlayerStats();
                for (DartStats ds : dsList4) {
                	dartStatsObj.insert(ds);
                }
            }        	
        }*/
        
    	return;
    }

    public void delete(long matchId) {
    	matchInfoObj.delete(matchId);
    	visitScoreObj.deleteAll(matchId);   
    	dartStatsObj.deleteAll(matchId);
    }

    
    public Match getResumable() {
    	long matchId= matchInfoObj.getResumableId();
    	if (matchId < 0)
    		throw new RuntimeException("No Resumable match found");
    	return get(matchId);
    }
    
    public boolean resumableExists() {
    	return matchInfoObj.resumableExists();
    }
    
    public void deleteAllResumable() {
    	long matchId= matchInfoObj.getResumableId();
    	if (matchId >= 0)
    		delete(matchId);
    }
               
}
